package com.weavernorth.sysap.checkstatus;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.finance.system.dao.LoginDao;
import com.weavernorth.sysap.util.GetUserMessageUtil;
import com.weavernorth.sysap.util.SyncUtil;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.log4j.Logger;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.matrix.MatrixUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: ecology_erdos
 * @description: 组织/岗位/人员检测接口;目的:预防同步后跟sap有状态不一致数据
 * @author: zm
 * @create: 2019-12-09 10:24
 **/
public class CheckSuccessStatus  {
    //    public static Log log = LogFactory.getLog(CheckSuccessStatus.class.getName());
    public Logger log;
    public Logger getLog() {
        return log;
    }

    public void setLog(Logger log) {
        this.log = log;
    }
    public void checkStatus() {
        //基础类
        BaseBean bb = new BaseBean();
        //检测类
        CheckUtil checkUtil = new CheckUtil();
        SyncUtil syncUtil = new SyncUtil();
        StringBuffer strResult = new StringBuffer();
        strResult.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:rfc:functions'>");
        strResult.append("<soapenv:Header/>");
        strResult.append("<soapenv:Body>");
        strResult.append("<urn:ZIF_FW_000_CHECK>");
        strResult.append("<IS_FW>X</IS_FW>");
        strResult.append("<IT_DATA>");
        //查询分部未被禁用的数据
        String strSubXml = checkUtil.getHrmsubcompanyXml("hrmsubcompany", "subcompanycode");
        strResult.append(strSubXml);
        //查询部门未被禁用的数据
        String strDepartXml = checkUtil.getHrmsubcompanyXml("Hrmdepartment", "departmentcode");
        strResult.append(strDepartXml);
        //查询岗位未被禁用的数据
        String strJobtitleXml = checkUtil.getHrmsubcompanyXml("HrmJobtitles", "jobtitlecode");
        strResult.append(strJobtitleXml);
        //查询人员在职数据
        String strResourceXml = checkUtil.getHrmsubcompanyXml("hrmresource", "workcode");
        strResult.append(strResourceXml);
        strResult.append("</IT_DATA>");
        strResult.append("<IT_DATA_JSON></IT_DATA_JSON>");
        strResult.append("</urn:ZIF_FW_000_CHECK>");
        strResult.append("</soapenv:Body>");
        strResult.append("</soapenv:Envelope>");
        log.info("检测同步xml:"+strResult.toString());
        //检测接口发送数据
        //获取检测接口同步地址
        String strCheckUrl = Util.null2String(bb.getPropValue("sapMess", "checkUrl"));
        JSONArray jsonArray = syncUtil.getSyncDataInfos(log,strCheckUrl, strResult.toString());
        //返回数据有值,则证明是需修改状态的数据
        int intJsonsize = jsonArray.size();
        log.info("数组长度:"+intJsonsize);
        if (intJsonsize > 0) {
            for (int index = 0; index < intJsonsize; index++) {
                JSONObject jsonObject = JSON.parseObject(jsonArray.get(index).toString());
                log.info("返回json:"+jsonObject.toString());
                //获取OA表名:推送时传送
                String strTableName = Util.null2String(jsonObject.get("zfw_t"));
                log.info("返回表名:" + strTableName);
                //编码
                String strCode = Util.null2String(jsonObject.get("objid"));
                log.info("返回code:" + strCode);
                //名称
                String strName = Util.null2String(jsonObject.get("name"));
                log.info("返回Name:" + strName);
                //离职日期
                String strLzrq=Util.null2String(jsonObject.getString("ctedt"));//CTEDT
                log.info("离职日期:"+strLzrq);

                String strChangeDate=changeDate(strLzrq);
                //修改状态
                checkUtil.updateStatus(strTableName, strCode,strName,strChangeDate);
            }

        }
        //矩阵同步
        MatrixUtil.sysDepartmentData();
        //分部清楚缓存
        syncUtil.clearCache(0);
        // 部门缓存清除类
        syncUtil.clearCache(1);
        //清除岗位缓存
        syncUtil.clearCache(2);
        //人员清除缓存
        syncUtil.clearCache(3);


    }


    /**
     * 日期格式转换
     *
     * @param strDate 待转化日期yyyyMMdd
     * @return 返回转换为String格式日期yyyy-MM-dd
     */
    private String changeDate(String strDate) {
        if ("00000000".equals(strDate)) {
            return "";
        }
//        log.info("待转化的日期:" + strDate);
        //返回日期格式
        String strReturnDate = "";
        //注意：SimpleDateFormat构造函数的样式与strDate的样式必须相符
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //加上时间
        //必须捕获异常
        try {
            Date date = simpleDateFormat.parse(strDate);
            strReturnDate = sDateFormat.format(date);
        } catch (ParseException px) {
            px.printStackTrace();
        }
//        log.info("转换后的日期:" + strReturnDate);
        return strReturnDate;

    }


}
